﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;

namespace Marbles.Resources
{
    /// <summary>
    /// Internal Resource Mapping. Manager to enable retrieval of Resource URI
    /// </summary>
    public static class ResourceHandler
    {

        private static bool IsInternetExplorer(Page page)
        {
            return page.Request.UserAgent.ToUpper().Contains("MSIE ");
        }

        public static string GetApplicationJavascript(Page page)
        {
            if (IsInternetExplorer(page))
            {
                return page.ClientScript.GetWebResourceUrl(typeof(ResourceHandler), "Marbles.Resources.Scripts.IE.application.js");
            }

            return page.ClientScript.GetWebResourceUrl(typeof(ResourceHandler), "Marbles.Resources.Scripts.Standards.application.js");
        }

        public static string GetContentEditorJavaScript(Page page)
        {
            if (IsInternetExplorer(page))
            {
                return page.ClientScript.GetWebResourceUrl(typeof(ResourceHandler), "Marbles.Resources.Scripts.IE.contentEditor.js");
            }

            return page.ClientScript.GetWebResourceUrl(typeof(ResourceHandler), "Marbles.Resources.Scripts.Standards.contentEditor.js");
        }

        public static string GetWebResourceUrl(Page page, string key)
        {            
            return page.ClientScript.GetWebResourceUrl(typeof(ResourceHandler), key);
        }

        public static string GetBuildVersion()
        {
            return ResourceUtility.GetEmbeddedResourceValue("Marbles.Resources.Content.BuildVersion.txt");
        }

        public static string GetLicenseText()
        {
            string value = ResourceUtility.GetEmbeddedResourceValue("Marbles.Resources.Content.License.txt");
            value = value.Replace("\r\n", "<br/>");
            return value.Trim();
        }

        public static string GetjQueryWysiwygColorPicker(Page page)
        {
            return page.ClientScript.GetWebResourceUrl(typeof(ResourceHandler), "Marbles.Resources.Scripts.jQuery.wysiwyg.colorpicker.js");
        }

        public static string GetGoogleAPIKey()
        {
            return ResourceUtility.GetEmbeddedResourceValue("Marbles.Resources.Content.GoogleAPIKey.txt").Trim();
        }

        public static string GetjQueryWysiwygImage(Page page)
        {
            return page.ClientScript.GetWebResourceUrl(typeof(ResourceHandler), "Marbles.Resources.Scripts.jQuery.wysiwyg.image.js");
        }

        public static string GetjQueryWysiwygLink(Page page)
        {
            return page.ClientScript.GetWebResourceUrl(typeof(ResourceHandler), "Marbles.Resources.Scripts.jQuery.wysiwyg.link.js");
        }

        public static string GetjQueryWysiwygTable(Page page)
        {
            return page.ClientScript.GetWebResourceUrl(typeof(ResourceHandler), "Marbles.Resources.Scripts.jQuery.wysiwyg.table.js");
        }

        public static string GetjQueryWysiwyg(Page page)
        {
            return page.ClientScript.GetWebResourceUrl(typeof(ResourceHandler), "Marbles.Resources.Scripts.jQuery.wysiwyg.jquery.js");
        }

        public static void RegisterWysiwygScripts(Page page)
        {
            LiteralControl lt = new LiteralControl();
            lt.Text = "<link href=\"" + page.ClientScript.GetWebResourceUrl(typeof(ResourceHandler), "Marbles.Resources.Scripts.jQuery.wysiwyg.style.css") + "\" type=\"text/css\" rel=\"stylesheet\" />";
            lt.ID = "wysiwygCSS";
            page.Header.Controls.Add(lt);
            if (!page.ClientScript.IsClientScriptIncludeRegistered("Wysiwyg")) { page.ClientScript.RegisterClientScriptInclude("Wysiwyg.ColorPicker", GetjQueryWysiwyg(page)); }
            if (!page.ClientScript.IsClientScriptIncludeRegistered("Wysiwyg.Image")) { page.ClientScript.RegisterClientScriptInclude("Wysiwyg.Image", GetjQueryWysiwygImage(page)); }
            if (!page.ClientScript.IsClientScriptIncludeRegistered("Wysiwyg.Link")) { page.ClientScript.RegisterClientScriptInclude("Wysiwyg.Link", GetjQueryWysiwygLink(page)); }
            if (!page.ClientScript.IsClientScriptIncludeRegistered("Wysiwyg.Table")) { page.ClientScript.RegisterClientScriptInclude("Wysiwyg.Table", GetjQueryWysiwygTable(page)); }
            if (!page.ClientScript.IsClientScriptIncludeRegistered("Wysiwyg.ColorPicker")) { page.ClientScript.RegisterClientScriptInclude("Wysiwyg.ColorPicker", GetjQueryWysiwygColorPicker(page)); }
        }

    }
}
